package org.itboys.mobile.controller.weixin;

import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
import org.itboys.commons.CommonConstants;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mobile.dto.SundayFootprintDto;
import org.itboys.mobile.dto.SundayMemberDto;
import org.itboys.mobile.dto.SundayMemberSessionDto;
import org.itboys.mobile.service.mongo.front.SundayFrontFootprintService;
import org.itboys.mobile.service.mongo.front.SundayFrontMemberService;
import org.itboys.mobile.util.AccountIdHoldUtils;
import org.itboys.mobile.util.ResponseMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zhangyang on 2018/1/4.
 * 圣代_用户_controller_weixin
 */
@Controller
@RequestMapping("/sunday/weixin/member")
public class SundayWeiXinMemberController extends BaseController{

    @Autowired
    private SundayFrontMemberService memberService;
    @Autowired
    private SundayFrontFootprintService footprintService;


    /**
     * 我的_主页
     * @param request
     * @param model
     * @return
     */
    @RequestMapping(value="/index", method= {RequestMethod.GET,RequestMethod.POST})
    public String index(
            HttpServletRequest request,Model model){
        try{
            Long memberId = AccountIdHoldUtils.getSessionMember(request).getId();
            SundayMemberDto member = memberService.packageDtoSingle(null,memberId,true,true);
            model.addAttribute("memberDto",member);
            return "/aigou/weixin/member/index";
        }catch (Exception e){
            logger.error("/sunday/weixin/member/index----e="+e.getMessage());
            e.printStackTrace();
            return "/aigou/weixin/error";
        }
    }


    /**
     * 收藏关注的用户
     * @param request
     * @return
     */
    @RequestMapping(value="/follow", method= {RequestMethod.GET,RequestMethod.POST})
    public String follow(
            HttpServletRequest request,Model model){
        try{
            SundayMemberSessionDto memberSessionDto =AccountIdHoldUtils.getSessionMember(request);
            List<SundayMemberDto> members = memberService.getCollection(request,memberSessionDto.getId());
            model.addAttribute("members",members);
            return "/aigou/weixin/member/follow";
        }catch (Exception e){
            logger.error("/sunday/weixin/member/follow----e="+e.getMessage());
            e.printStackTrace();
            return "/aigou/weixin/error";
        }
    }

    /**
     * 足迹页
     * @param request
     * @return
     */
    @RequestMapping(value="/footprint/index", method= {RequestMethod.GET,RequestMethod.POST})
    public String footprint(
            HttpServletRequest request){
        AccountIdHoldUtils.getSessionMember(request);
        return "/aigou/weixin/member/footprint/index";
    }

    /**
     * 获取足迹数据
     * @param request
     * @param model
     * @return
     */
    @RequestMapping(value="/footprint/select", method= RequestMethod.POST)
    @ApiOperation(value="【微信端】获取足迹数据")
    public String footprintSelect(
            HttpServletRequest request,Model model){
        try{
            Long memberId = AccountIdHoldUtils.getSessionMember(request).getId();
            List<SundayFootprintDto> list = footprintService.select(memberId,request);
            model.addAttribute("list",list);
            return "/aigou/weixin/member/footprint/data";
        }catch (Exception e){
            logger.error("/sunday/weixin/member/footprint/select----e="+e.getMessage());
            e.printStackTrace();
            return null;
        }
    }

    /**
     *  修改个人信息。-1直接提示消息；
     * @param request
     * @param nickName
     * @param mobileName
     * @param address
     * @param email
     * @param image
     * @param idCard
     * @param desc
     */
    @RequestMapping(value="/update", method= {RequestMethod.GET,RequestMethod.POST})
    @ApiOperation(value="【微信端】修改个人信息。")
    public void update(HttpServletRequest request,
                       HttpServletResponse response,
                       @ApiParam(required=false, name="nickName", value="昵称")@RequestParam(value = "nickName", required = false)String nickName,
                       @ApiParam(required=false, name="mobileName", value="联系电话")@RequestParam(value = "mobileName", required = false)String mobileName,
                       @ApiParam(required=false, name="address", value="地址")@RequestParam(value = "address", required = false)String address,
                       @ApiParam(required=false, name="email", value="电子邮箱")@RequestParam(value = "email", required = false)String email,
                       @ApiParam(required=false, name="image", value="头像")@RequestParam(value = "image", required = false)String image,
                       @ApiParam(required=false, name="idCard", value="身份证号码")@RequestParam(value = "idCard", required = false)String idCard,
                       @ApiParam(required=false, name="desc", value="个人简介")@RequestParam(value = "desc", required = false)String desc,
                       @ApiParam(required=false, name="birthDate", value="生日")@RequestParam(value = "birthDate", required = false)String birthDate,
                       @ApiParam(required=false, name="sex", value="性别。0女1男2保密")@RequestParam(value = "sex", required = false)Integer sex){


        try{
            Long memberId = AccountIdHoldUtils.getSessionMember(request).getId();
            SundayMemberDto result =memberService.update( memberId, nickName, mobileName, address, email, image, idCard, desc, birthDate, sex);
            ResponseMessage.success(result);
        }catch (Exception e){
            String msg =CommonConstants.ERROMSG;
            Integer code=CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code=CommonConstants.TOKEN_CODE;
                msg=CommonConstants.TOKENMSG;
            }else{
                logger.error("/sunday/weixin/member/update----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code,msg);
        }
    }

    /**
     * 分享页
     * @param model
     * @return
     */
    @RequestMapping(value="/share", method= {RequestMethod.GET,RequestMethod.POST})
    public String share(Model model,
            @RequestParam(value = "memberId", required = false)Long memberId){
        try{
            //SundayMemberSessionDto member=
            model.addAttribute("memberDto",memberService.share(memberId));
            return "/aigou/weixin/member/share";
        }catch (Exception e){
            logger.error("/sunday/weixin/member/share----e="+e.getMessage());
            e.printStackTrace();
            return "/aigou/weixin/error";
        }
    }


    /**
     * 我的会员页面
     * @param request
     * @param model
     * @return
     */
    @RequestMapping(value="/leaguer/index", method= {RequestMethod.GET,RequestMethod.POST})
    public String leaguer_index(
            HttpServletRequest request, Model model){
        try{
            Long memberId = AccountIdHoldUtils.getSessionMember(request).getId();
            //SundayMemberDto member = memberService.packageDtoSingle(null,memberId,false,false);
            //model.addAttribute("memberDto",member);
            Map<String,Object> result = memberService.getLeaguer(memberId);
            model.addAttribute("result",result);
            return "/aigou/weixin/member/leaguer/index";
        }catch (Exception e){
            logger.error("/sunday/weixin/member/leaguer/index----e="+e.getMessage());
            e.printStackTrace();
            return "/aigou/weixin/error";
        }
    }

  /*  @RequestMapping(value="/leaguer/select", method= RequestMethod.POST)
    @ApiOperation(value="【微信端】获取我的会员数据")
    public String leaguerSelect(
            HttpServletRequest request,Model model,
            @ApiParam(required = true, name = "type", value = "类型:1-获取一级会员,2-获取二级会员")@RequestParam(value = "type", required = true)Integer type,
            @ApiParam(required = true, name = "pageSize", value = "分页参数。每页数量。默认20") @RequestParam(value = "pageSize", required = true) String pageSize,
            @ApiParam(required = true, name = "pageNumber", value = "分页参数。第几页。默认1") @RequestParam(value = "pageNumber", required = true) String pageNumber,
            @ApiParam(required = false, name = "sort", value = "排序参数。字段名称。默认为空") @RequestParam(value = "sort", required = false) String sort,
            @ApiParam(required = false, name = "order", value = "排序参数。。默认为空。ASC正序/DESC倒序") @RequestParam(value = "order", required = false) String order){
        try{
            Long memberId = AccountIdHoldUtils.getSessionMember(request).getId();
            List<SundayMemberDto> list = memberService.getLeaguerData(request,memberId,type);
            model.addAttribute("list",list);
            return "/aigou/weixin/member/leaguer_data";
        }catch (Exception e){
            logger.error("/sunday/weixin/member/leaguer/select----e="+e.getMessage());
            e.printStackTrace();
            return null;
        }
    }*/
    @ApiOperation(value="【微信端】客服页面（暂时静态页面）")
    @RequestMapping(value="/chat", method= {RequestMethod.GET,RequestMethod.POST})
    public String chat(
            HttpServletRequest request,
            Model model){
        try{
            Long memberId = AccountIdHoldUtils.getSessionMember(request).getId();
            //SundayMemberDto member = memberService.packageDtoSingle(null,memberId,true,true);
          //  model.addAttribute("memberDto",member);
            return "/aigou/weixin/member/chat";

        }catch (Exception e){
            String msg = CommonConstants.ERROMSG;
            Integer code= CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code= CommonConstants.TOKEN_CODE;
                msg= CommonConstants.TOKENMSG;
            }else{
                logger.error("/sunday/weixin/member/chat----e="+e.getMessage());
            }
            e.printStackTrace();
            return "/aigou/weixin/error";
        }
    }
}
